md_add_item() {
    local relative_path=${1:?}
    local filename=${2:?}
    local indentation=${3:-}


    echo "${indentation}- [${filename}]([${relative_path}])"
}


org_add_item() {
    local relative_path=${1:?}
    local filename=${2:?}
    local indentation=${3:-}


    echo "${indentation}- [[${relative_path}][${filename}]]"
}


build_sitemap_from_utils() {
    local add_item_func=${1:?}
    local offset=${2:?}
    local absolute_working_dir=${3:?}


    local find_results=()
    while IFS=  read -r -d $'\0'; do
        find_results+=("$REPLY")
    done < <(find "${absolute_working_dir}" -type d \
                  -not -path "${absolute_working_dir}/drafts" \
                  -not -path "${absolute_working_dir}/drafts/*" \
                  -not -path "${absolute_working_dir}/static" \
                  -not -path "${absolute_working_dir}/static/*" \
                  -print0)
    unset IFS
    local directories=()
    readarray -t directories < <(printf '%s\n' "${find_results[@]}" | sort)
    for current_dir in "${directories[@]}"
    do
        if [[ $current_dir == "$absolute_working_dir" ]]; then
            eval "${add_item_func} \"${offset}/\" \"${offset}/home\""
        else
            eval "${add_item_func} \
                      ${current_dir/$absolute_working_dir/$offset} \
                      ${current_dir/$absolute_working_dir/$offset}"
        fi


        local find_files=()
        while IFS=  read -r -d $'\0'; do
            find_files+=("$REPLY")
        done < <(find "${current_dir}" -maxdepth 1 -type f \
                      -not -path "${absolute_working_dir}/drafts/*" \
                      -not -path "${absolute_working_dir}/static/*" \
                      -not -name ".htaccess" \
                      -not -name "*.css" \
                      -print0)
        unset IFS
        local files_sorted=()
        readarray -t files_sorted < <(printf '%s\n' "${find_files[@]}" | sort)
        for current_file in "${files_sorted[@]}"
        do
            if [[ $current_file != "" ]] && \
                   [[ "$current_file" != "$absolute_working_dir" ]]; then
                eval "${add_item_func} \
                          ${current_file/$absolute_working_dir/$offset} \
                          $(basename "$current_file") \
                          \"  \""
            fi
        done
    done
}


build_sitemap_procedure() {
    local template=${1:?}
    local output_file=${2:?}
    local offset=${3:?}
    local absolute_working_dir=${4:?}
    local ext=${5:?}

    
    cp "${template}" "${output_file}"


    if [[ "$ext" == "md" ]]; then
        build_sitemap_from_utils \
            "md_add_item" "${offset}" "${absolute_working_dir}" >> "${output_file}"
    elif  [[ "$ext" == "org" ]]; then
        build_sitemap_from_utils \
            "org_add_item" "${offset}" "${absolute_working_dir}" >> "${output_file}"
    else
        >&2 echo "ERROR: ${FUNCNAME[0]}: unsupported template extension: \"${ext}\""
    fi
}


build_sitemap_main() {
    local input_dir=${1:?}
    local template=${2:?}
    local output_file=${3:?}
    local sitemap_offset_from_root=${4:?}


    build_sitemap_procedure \
        "${template}" \
        "${output_file}" \
        "${sitemap_offset_from_root}" \
        "$(get_absolute_path_better "$input_dir")" \
        "$(get_file_ext "${template}")"
}
